﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using KL.UFrame.Core.Result;
using KL.UFrame.Data.Business.Account;
using KL.UFrame.Data.Models.Account;

namespace KL.UFrame.Api.ApiControllers
{
    /// <summary>
    /// 用户管理接口
    /// </summary>
    [RoutePrefix("api/User")]
    public class UserController : K9ApiBaseController
    {
        private IAccountBusiness _business;

        public UserController(IAccountBusiness business)
        {
            _business = business;
        }

        /// <summary>
        /// 分页查询所有用户列表
        /// </summary>
        /// <param name="phone"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        public async Task<ResultMessage<List<UserInfoModel>>> Get(string phone, int pageIndex, int pageSize)
        {
            if (pageSize <= 0)
            {
                pageSize = 10;
            }
            if (pageIndex < 1)
            {
                pageIndex = 1;
            }
            return await _business.GetAllAsync(phone, pageIndex, pageSize);
        }

        /// <summary>
        /// 切换用户锁定状态
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [Route("Lock")]
        public async Task<ResultMessage> Lock(string userId)
        {
            var result = await _business.ToggleLockAsync(userId);
            return result ? new ResultMessage() : new ResultMessage(-500, "变更用户锁定状态失败");
        }

        /// <summary>
        /// 切换用户角色
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="role"></param>
        /// <returns></returns>
        [Route("Role")]
        public async Task<ResultMessage<bool>> Role(string userId, string role)
        {
            var result = await _business.ToggleRoleAsync(userId, role);
            return new ResultMessage<bool>(result);
        }

        /// <summary>
        /// 获取用户角色
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [Route("Role")]
        public async Task<ResultMessage<List<UserRoleModel>>> GetRoles(string userId)
        {
            var result = await _business.GetRolesAsync(userId);
            return new ResultMessage<List<UserRoleModel>>(result);
        }

        /// <summary>
        /// 创建角色
        /// </summary>
        /// <param name="role"></param>
        /// <returns></returns>
        [Route("Role")]
        public async Task<ResultMessage> CreateRole(string role)
        {
            var result = await _business.CreateRoleAsync(role);
            return new ResultMessage();
        }

        /// <summary>
        /// 后台创建用户
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<ResultMessage> CreateUser(UserRegisterModel model)
        {
            var userId = await _business.CreateUserFromBackendAsync(model);
            return new ResultMessage(0, userId);
        }
    }
}
